//*************************************************************************************************
//Copyright (c) 2015.year. @hjjer.
//If you are a hjjer(hejianjiao.org), This code is unlicensed, otherwise,is licensed under the GPL.
//*************************************************************************************************

package com.truthbean.ssm.demo.service.impl;

import com.truthbean.ssm.core.utils.StringUtil;
import com.truthbean.ssm.demo.domain.entity.Enroll;
import com.truthbean.ssm.demo.persistence.EnrollMapper;
import com.truthbean.ssm.demo.service.EnrollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Truthbean
 * @since 2015-12-16 20:16:20
 */
@Service
public class EnrollServiceImpl implements EnrollService {

    @Autowired
    private EnrollMapper enrollMapper;

    @Override
    public boolean save(Enroll enroll) {
        return enrollMapper.save(enroll) > 0;
    }

    @Override
    public boolean updateArea(String newArea, String oldArea) {
        return enrollMapper.updateArea(newArea, oldArea) > 0;
    }

    @Override
    public boolean deleteEnroll(String area) {
        return enrollMapper.deleteEnroll(area) > 0;
    }

    @Override
    public boolean updateYear(int oldYear, String enrollArea, String newYear) {
        if (oldYear > 0) {
            String temp = enrollMapper.queryYearByArea(enrollArea);
            String oldYearStr = "," + oldYear;
            if (!temp.matches(oldYearStr)) {
                oldYearStr = String.valueOf(oldYear);
            }
            if (StringUtil.isEmpty(newYear)) {
                return enrollMapper.updateYear(oldYearStr, enrollArea, "") > 0;
            } else {
                return enrollMapper.updateYear(oldYearStr, enrollArea, newYear) > 0;
            }
        }
        return false;
    }

    @Override
    public boolean addYear(String enrollArea, int newYear) {
        if (newYear > 0) {
            String newYearStr = "," + newYear;
            return enrollMapper.addYear(enrollArea, newYearStr) > 0;
        }
        return false;
    }

    @Override
    public Set<String> queryYearsOverPage(int begin, int size) {
        List<String> tmp = enrollMapper.queryYearsOverPage(begin, size);
        Set<String> temp = new HashSet<>();
        Set<String> result = new HashSet<>();
        temp.addAll(tmp);
        temp.stream().filter((str) -> (str.contains(","))).
                map((str) -> str.split(",")).forEach((strs) -> {
            result.addAll(Arrays.asList(strs));
        });
        return result;
    }

    @Override
    public Set<String> queryAllYears() {
        List<String> tmp = enrollMapper.queryAllYears();
        Set<String> temp = new HashSet<>();
        Set<String> result = new HashSet<>();
        temp.addAll(tmp);
        temp.stream().filter((str) -> (str.contains(","))).
                map((str) -> str.split(",")).forEach((strs) -> {
            result.addAll(Arrays.asList(strs));
        });
        return result;
    }

    @Override
    public List<String> queryAreasOverPage(int begin, int size) {
        return enrollMapper.queryAreasOverPage(begin, size);
    }

    @Override
    public List<String> queryAllAreas() {
        return enrollMapper.queryAllAreas();
    }

    @Override
    public int[] queryYearByArea(String area) {
        int[] years = null;
        if (StringUtil.isNotEmpty(area)) {
            String temp = enrollMapper.queryYearByArea(area);
            if (StringUtil.isNotEmpty(temp)) {
                String[] results = temp.split(",");
                years = new int[results.length];
                for (int i = 0; i < results.length; i++) {
                    years[i] = Integer.parseInt(results[i]);
                }
            }
        }
        return years;
    }

    @Override
    public boolean batchDeleteEnroll(String[] area) {
        List<String> in = new ArrayList<>();
        in.addAll(Arrays.asList(area));
        return enrollMapper.batchDeleteEnroll(in) == area.length;
    }

    @Override
    public boolean batchDeleteYear(String[] area, String year) {
        List<String> in = new ArrayList<>();
        in.addAll(Arrays.asList(area));
        return enrollMapper.batchDeleteYear(in, year) == area.length;
    }

    @Override
    public boolean batchAddYear(String[] enrollArea, int newYear) {
        List<String> in = new ArrayList<>();
        in.addAll(Arrays.asList(enrollArea));
        return enrollMapper.batchAddYear(in, newYear) == enrollArea.length;
    }

    @Override
    public Map<String, String[]> queryEnrollOverPage(int begin, int size) {
        List<Enroll> temp = enrollMapper.queryEnrollOverPage(begin, size);
        Map<String, String[]> result = new HashMap<>();
        temp.stream().forEach((enroll) -> {
            result.put(enroll.getEnrollArea(), enroll.getYearsString().split(","));
        });
        return result;
    }

    @Override
    public Map<String, String[]> queryAll() {
        List<Enroll> tmp = enrollMapper.queryAll();
        Map<String, String[]> result = new HashMap<>();
        tmp.stream().forEach((enroll) -> {
            result.put(enroll.getEnrollArea(), enroll.getYearsString().split(","));
        });
        return result;
    }

    @Override
    public int countEnroll() {
        return enrollMapper.countEnroll();
    }

    @Override
    public boolean isAreaExist(String area) {
        return enrollMapper.isAreaExist(area);
    }

}
